import 'package:mirror/api/UserApi.dart';
import 'package:mirror/bean/ChatBean.dart';
import 'package:mirror/bean/GroupBean.dart';
import 'package:mirror/bean/GroupBean2.dart';
import 'package:mirror/service/NetworkService.dart';

class MessageApi {
  static const _createRoomUrl = "Chat-Service/chatRoom/add";
  static const _getAllOfMyGroups = "Chat-Service/chatRoom/uList/";
  static const _getAllGroups = "Chat-Service/chatRoom/list";
  static const _addGroupUrl = "Chat-Service/chatRoomUser/add";
  static const _getRoomMsgUrl = "Chat-Service/chatRoom/get/";

  static const _getHistoryUrl = "Chat-Service/message/list/";

  static Future createRoom({required String roomName}) async {
    await NetworkService.post(url: _createRoomUrl, map: {
      "roomName": roomName,
      "createName": "${(await UserApi.getUser())?.userUid}"
    });
  }

  static Future<List<GroupBean>?> getAllOfMyGroups() async {
    var res = await NetworkService.post(
        url: "$_getAllOfMyGroups${(await UserApi.getUser())?.userUid}",
        map: {});
    return res.getTypedList<GroupBean>((map) => GroupBean.fromJson(map));
  }

  static Future<List<GroupBean>?> getAllGroups() async {
    var res = await NetworkService.post(url: _getAllGroups, map: {});
    return res.getTypedList<GroupBean>((map) => GroupBean.fromJson(map));
  }

  static addGroup(
      {required String roomId,
      required String userType,
      required String roomName,
      required String passwd}) async {
    var res = await NetworkService.post(url: _addGroupUrl, map: {
      "roomId": roomId,
      "userId": "${(await UserApi.getUser())?.userUid}",
      "userType": userType,
      "roomName": roomName,
      "passwd": passwd
    });
    print(res.getTypedList<ChatBean>((map) => ChatBean.fromJson(map)));
  }

  static Future<GroupBean2?> getRoomMsg({required String roomId}) async {
    var res = await NetworkService.post(url: "$_getRoomMsgUrl$roomId", map: {});
    return res.getTypedData((map) => GroupBean2.fromJson(map));
  }

  static Future<List<ChatBean>?> getHistory(
      {required String roomId,
      required int pageNumber,
      required String time}) async {
    var res = await NetworkService.post(
        url: "$_getHistoryUrl$roomId/$pageNumber/$time", map: {});
    return res.getTypedList<ChatBean>((map) => ChatBean.fromJson(map));
  }
}
